import { getApi } from "src/http"
import { accountParse } from "src/utils/accountUtils"
// import { test } from "src/utils/jettonUtils"
//引入全局变量
const ENV = import.meta.env
//初始化
export const init = ()=>{
    // let op = accountParse("UQAxp7JMc1uNTO8mLdnmwzCLmqIRUTZhNvDsKHJIUedg_zaa")
    // console.log("账户：",op)
    //先删除掉缓存
    window.localStorage.removeItem("ton-connect-storage_order");

    //test()
    console.log("全局环境:",ENV)
    // console.log("你前端用的啥框架 ")
   //let str = accountParse("0:31a7b24c735b8d4cef262dd9e6c3308b9aa21151366136f0ec28724851e760ff");
    //console.log(str,"====>>>>")
    addElementListener() //动态元素监听
    let orderId = document.location.pathname.substring(1)
    setLoadStu() //设置加载状态
    //截取末尾订单号
    console.log("订单号:",orderId)
    if(orderId.length>5){
        document.querySelector(".order-id").innerHTML="OrderNumber："+orderId
        //校验订单是否有效
        console.log("发送订单消息")//process.env

        // 使用示例
        return getApi(`${ENV.VITE_BASE_API}order/query?number=`+orderId)
          //   .then(data => {
          //   console.log(data)
          //   if(data.code===200){
          //       console.log(300000)
          //       setActiveStu()
          //       //写入缓存
          //       window.localStorage.setItem(orderId, JSON.stringify(data.data));
          //   }
          //   console.log(data);
          // }).catch(error => {
          //   console.error(error);
          //   return new Promise(()=>{})
          // });
    }else{
     console.log("无效的订单")
     return new Promise(()=>{})
    }
}

function setLoadStu(){
      document.querySelector(".order-id").style="color:rgb(222 50 64);font-weight:600"

      //将按钮置灰
      Array.from(document.querySelectorAll(".active-button")).map(v=>{
          //设置背景灰色并且不可点击
              v.style="background-color: #f1f3f5;color:rgb(0, 0, 0);margin-top: 15px;pointer-events: none;"
      })
      document.querySelector("#send-tx-ico").style.fill="rgb(0, 0, 0)";
      document.querySelector(".explain").style.display="none"  //屏蔽最后一个按钮
      document.querySelector(".order-id").innerHTML="Load data... Wait a moment, please."
      document.getElementById("payment-status").style="pointer-events: none;"
}

export const setActiveStu = ()=>{
      document.querySelector(".order-id").style="color:rgb(0 0 0);font-weight:600"
      //将按钮置灰
      Array.from(document.querySelectorAll(".active-button")).map(v=>{
          //设置背景灰色并且不可点击
          v.style="background-color: #f1f3f5;color:rgb(0, 0, 0);margin-top: 15px;pointer-events: none;"
      })
      document.querySelector("#button-link").style="background-color: rgb(0 152 234);color:#FFFFFF;margin-top: 15px;"
      document.querySelector("#send-tx-ico").style.fill="rgb(0, 0, 0)";
      document.querySelector(".explain").style.display="flex"  //显示最后一个
}

function setPassiveStu(){
      document.querySelector(".order-id").innerHTML="Invalid Order...."
      document.querySelector(".order-id").style="color:rgb(222 50 64);font-weight:600"

      //将按钮置灰
      Array.from(document.querySelectorAll(".active-button")).map(v=>{
          //设置背景灰色并且不可点击
          v.style="background-color: #f1f3f5;color:rgb(0, 0, 0);margin-top: 15px;pointer-events: none;"
      })
      document.querySelector("#send-tx-ico").style.fill="rgb(0, 0, 0)";
      document.querySelector(".explain").style.display="none"  //屏蔽最后一个按钮
}

/**
 * 元素变动监听
 */
function addElementListener(){
    //动态监听元素
    //监听举例-1
    var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver// Firefox和Chrome早期版本中带有前缀
    var target = document.body; // 选择目标节点*这里必须要指定监听的节点

    var observer = new MutationObserver(function(mutations) { // 创建观察者对象
    mutations.forEach(function(mutation) {
        if(mutation.addedNodes[0]){
             let ele = mutation.addedNodes[0]
             //console.log(ele.firstChild)
            if(ele.firstChild){
                let ele = mutation.addedNodes[0].firstChild.parentElement
                console.log(ele)
            }else{
               ele.remove()
            }
            let node = mutation.addedNodes[0]

            // ele.querySelector("")
            if(ele.querySelector("tc-root")){
                let rootEle = ele.querySelector("tc-root")
                //
                let isOpen = rootEle.innerHTML.includes("data-tc-list-notifications");
                if(isOpen){
                   ele.remove(); //删除新元素
                    let addEle =  document.querySelector("#button-root");
                    addEle.append(node)
                    //console.log("新增的元素",rootEle)
                    //复制元素
                }
            }
            // if(mutation.addedNodes[0].querySelector("")){
            //     console.log("动态添加：",mutation.addedNodes[0])
            // }
            //  console.log("动态添加：",mutation.addedNodes[0])
        };
     //&& mutation.addedNodes[0].remove();//这里的addedNodes是 返回添加的元素   mutation.addedNodes[0].remove()是删除所添加的元素

    });
    });
    var config = { attributes: true, childList: true, characterData: true } // 配置观察选项:
    observer.observe(target, config); // 传入目标节点和观察选项
}